﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Web;
using System.Web.Caching;

namespace DDNY.WebPortals.Common
{
    public class LinkManager
    {
        public static List<WebLink> GetAllLinks()
        {
            List<WebLink> result = new List<WebLink>();
            //result.Add(new WebLink("http://rsstrends.com","Latest Web Trends"));
            //result.Add(new WebLink("http://www.buymegadget.com","Easy Shopping"));
            //result.Add(new WebLink("http://bgsignal.com", "Новини"));
            //result.Add(new WebLink("http://symbolbrowse.com", "Financial Stock News"));
            //result.Add(new WebLink("http://gotocountries.com", "World Tourism Portal"));
            //result.Add(new WebLink("http://carnewsdaily.com", "Automotive News"));

            //result.Add(new WebLink("http://sportvisual.com/", "Sports Portal"));
            //result.Add(new WebLink("http://nieuwsnl.info/", "Laatste Nieuws"));
            //result.Add(new WebLink("http://forumsynd.com/", "Българските форуми"));
            result.Add(new WebLink("http://www.nakreviews.com/", "Single Store"));
            result.Add(GetRandomResLink());
            //result.Add(resLinks[randNumber]);
            return result;
        }

        public static List<WebLink> GetResLinks()
        {
            List<WebLink> result = new List<WebLink>();
            result.Add(new WebLink("http://www.restrends.com", "Renewable Energy Sources"));
            //result.Add(new WebLink("http://www.restrends.com", "Renewable Energy "));
            result.Add(new WebLink("http://www.restrends.com", "Renewable Energy"));
            result.Add(new WebLink("http://www.restrends.com/2011/11/07/restrends-all-you-need-to-know/", "Renewable Energy Sources"));
            //result.Add(new WebLink("http://www.restrends.com/2008/05/12/ultimate-restrends-guide/", "Ultimate Renewable Energy Sources Guide"));
            result.Add(new WebLink("http://www.restrends.com/2008/08/12/the-cost-of-renewable-energy/", "The Cost Of Renewable Energy"));
            result.Add(new WebLink("http://www.restrends.com/2009/04/06/best-solar-pv-panels-how-to-make-your-choice/", "Best Solar Panels"));
            result.Add(new WebLink("http://www.restrends.com/2009/04/06/best-solar-pv-panels-how-to-make-your-choice/", "Best Solar Panels"));
            //result.Add(new WebLink("http://www.restrends.com/2010/10/15/restrends-what-are-they/", "Renewable Energy Sources – What Are They?"));
            //result.Add(new WebLink("http://www.restrends.com/2009/10/06/all-restrends-in-briefe/", "All Renewable Energy Sources in Brief"));
            result.Add(new WebLink("http://www.restrends.com/2011/11/07/restrends-all-you-need-to-know/", "Renewable Energy Sources"));
            result.Add(new WebLink("http://www.restrends.com/2009/04/06/best-solar-pv-panels-how-to-make-your-choice/", "Best Photovoltaic Panels"));
            result.Add(new WebLink("http://www.restrends.com/2009/01/20/solar-power-equipment-my-list-of-best-solar-panel-manufacturers/", "Best Solar Panel Manufacturers – My List Of Top Photovoltaic Energy Equipment Providers"));
            result.Add(new WebLink("http://www.restrends.com/2009/01/20/solar-power-equipment-my-list-of-best-solar-panel-manufacturers/", "Best PV Panel Manufacturers – My List Of Top Photovoltaic Energy Equipment Providers"));
            result.Add(new WebLink("http://www.restrends.com/2008/06/11/advantages-and-disadvantages-of-solar-power/", "Advantages And Disadvantages Of Solar Power"));
            result.Add(new WebLink("http://www.restrends.com/2009/04/06/best-solar-pv-panels-how-to-make-your-choice/", "Best Solar Modules"));
            return result;
        }

        public static WebLink GetRandomResLink()
        {
            WebLink result = null;
            Random randFeedOrLink = new Random();
            int intFeedOrLink = randFeedOrLink.Next(0, 100);
            //on 30% of the pages I want to have a link to some item in the RSS
            if (intFeedOrLink < 85)
            {
                Random rand = new Random();
                List<WebLink> tempResult = GetResLinks();
                int randomInt = rand.Next(0, tempResult.Count);
                result = tempResult[randomInt];
            }
            else
            {
                try
                {
                    string cacheKey = "RESFeedKey";
                    SyndicationFeed syndicationFeed = HttpContext.Current.Cache[cacheKey] as SyndicationFeed;
                    if (syndicationFeed==null)
                    {
                        string feedUrl = "http://feeds.feedburner.com/therenewableenergysources";
                        XmlReader reader = XmlReader.Create(feedUrl);
                        syndicationFeed = SyndicationFeed.Load(reader);
                        HttpContext.Current.Cache.Add(cacheKey, syndicationFeed, null, DateTime.Now.AddHours(4), TimeSpan.Zero, CacheItemPriority.High, null);
                    }
                    
                    Random rand = new Random();
                    int randomInt = rand.Next(0, syndicationFeed.Items.Count()-1);
                    SyndicationItem syndItem = syndicationFeed.Items.ToList()[randomInt];
                    result = new WebLink(syndItem.Id,syndItem.Title.Text);
                }
                catch (Exception)
                {
                    Random rand = new Random();
                    List<WebLink> tempResult = GetResLinks();
                    int randomInt = rand.Next(0, tempResult.Count);
                    result = tempResult[randomInt];
                }

            }

            return result;

        }
    }
}
